package com.htdata.htiiot.conf;

import java.io.*;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.zookeeper.KeeperException;



public class Configs {
	
	private String jarAbsPath;
	private String propConfFile;
	public  String zookeeper = null;
	public  String redisDir = null;
	public  String mysqlDir = null;
	private String mysqlUrl = null;
	private String mysqlUser  = null;
	private String mysqlPassword  = null;
	private String mysqlDriver  = null;
	private String redisUrl  = null;
	

	private String redisPort  = null;
	private String redisPassword  = null;

	public URI uri = null;
	public File file = null;
	
	public Configs() {


		try {
			
			uri = Configs.class.getClassLoader().getResource("").toURI();
			
			System.out.println("uri："+uri);
			file = new File(uri);
			if (file.isFile() || file.getPath().endsWith(".jar") || (".zip").equals(file.getPath())) {
				jarAbsPath = file.getParent();
			} else {
				jarAbsPath = file.getPath();
			}
			System.setProperty("jarAbsPath", jarAbsPath);


			propConfFile = Paths.get(jarAbsPath, "conf", "app.properties").toString();

			loadContent();
			getConfigs();
		} catch (Exception e) {
			
		//	System.exit(0);
		}
	}

	private void loadContent() throws Exception {
		File file = new File(propConfFile);
		FileInputStream fStream = new FileInputStream(file);
		InputStreamReader inStream =  new InputStreamReader(fStream,"UTF-8");
		BufferedReader bReader = new BufferedReader(inStream);

		Properties properties = new Properties();
		properties.load(bReader);
		
		zookeeper = properties.getProperty("htiiot.zookeeper", "192.168.0.79:2181");
		if (null == zookeeper || zookeeper.isEmpty()) {
			throw new ExceptionInInitializerError("htiiot.zookeeper in '" + propConfFile + "' must not be empty!");
		} else {
			zookeeper = zookeeper.trim();
			System.out.println("zookeeper: "+zookeeper);
		}

		redisDir = properties.getProperty("zk.redisDir", "/conf_htiiot/redis/");
		if (null == redisDir || redisDir.isEmpty()) {
			throw new ExceptionInInitializerError("zk.redisDir in '" + propConfFile + "' must not be empty!");
		} else {
			redisDir = redisDir.trim();
			System.out.println("redisDir: "+redisDir);
		}
		mysqlDir = properties.getProperty("zk.mysqlDir", "/conf_htiiot/maindb/");
		if (null == mysqlDir || mysqlDir.isEmpty()) {
			throw new ExceptionInInitializerError("zk.mysqlDir in '" + propConfFile + "' must not be empty!");
		} else {
			mysqlDir = mysqlDir.trim();
			System.out.println("mysqlDir: "+mysqlDir);
		}

	

	}

	
	public void getConfigs() throws IOException, InterruptedException, KeeperException{
		
		 ZooKeeperOperator zkoperator  = new ZooKeeperOperator();   
		 zkoperator.connect(zookeeper);
		 
		  mysqlUrl 		= zkoperator.getData(mysqlDir+"url");
		  mysqlUser 	= zkoperator.getData(mysqlDir+"user");
		  mysqlPassword = zkoperator.getData(mysqlDir+"sec.password");
		  mysqlDriver 	= zkoperator.getData(mysqlDir+"driverclassname");
		  redisUrl 		= zkoperator.getData(redisDir+"address");
		  redisPort 	= zkoperator.getData(redisDir+"port");
		  redisPassword = zkoperator.getData(redisDir+"auth");
	}
	
	public String getJarAbsPath() {
		return jarAbsPath;
	}

	public void setJarAbsPath(String jarAbsPath) {
		this.jarAbsPath = jarAbsPath;
	}

	public String getPropConfFile() {
		return propConfFile;
	}

	public void setPropConfFile(String propConfFile) {
		this.propConfFile = propConfFile;
	}

	public  String getZookeeper() {
		return zookeeper;
	}

	public  void setZookeeper(String zookeeper) {
		this.zookeeper = zookeeper;
	}

	public String getMysqlUrl() {
		return mysqlUrl;
	}

	public void setMysqlUrl(String mysqlUrl) {
		this.mysqlUrl = mysqlUrl;
	}

	public String getMysqlUser() {
		return mysqlUser;
	}

	public void setMysqlUser(String mysqlUser) {
		this.mysqlUser = mysqlUser;
	}

	public String getMysqlPassword() {
		return mysqlPassword;
	}

	public void setMysqlPassword(String mysqlPassword) {
		this.mysqlPassword = mysqlPassword;
	}

	public String getMysqlDriver() {
		return mysqlDriver;
	}

	public void setMysqlDriver(String mysqlDriver) {
		this.mysqlDriver = mysqlDriver;
	}

	public String getRedisUrl() {
		return redisUrl;
	}

	public void setRedisUrl(String redisUrl) {
		this.redisUrl = redisUrl;
	}

	public String getRedisPort() {
		return redisPort;
	}

	public void setRedisPort(String redisPort) {
		this.redisPort = redisPort;
	}

	public String getRedisPassword() {
		return redisPassword;
	}

	public void setRedisPassword(String redisPassword) {
		this.redisPassword = redisPassword;
	}

	public URI getUri() {
		return uri;
	}

	public void setUri(URI uri) {
		this.uri = uri;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}




	
}
